package com.learn.project.plan.bean;


import java.util.Arrays;
import java.util.List;

/**
 * @Description: 循环计划
 * @Author: chenfuyuan
 * @Date: 2021/4/27 20:45
 */
public class CyclePlan implements Plan {

    private static final long serialVersionUID = 5101116573098952230L;
    /**
     * 任务列表
     */
    private List<Task> tasks;

    /**
     * 当前任务
     */
    private int currentTaskIndex = -1;



    @Override
    public Task[] getTasks() {
        if (isEmpty()) {
            return new Task[0];
        }
        return tasks.toArray(new Task[0]);
    }

    @Override
    public void setTasks(Task[] tasks) {
        this.tasks = Arrays.asList(tasks);
        currentTaskIndex = 0;
    }


    @Override
    public Task getCurrentTask() {
        if (isEmpty()) {
            throw new RuntimeException("任务清单为空，请补充");
        }
        return tasks.get(currentTaskIndex);
    }


    @Override
    public void finishTask() {
        if (isEmpty()) {
            throw new RuntimeException("任务清单为空，请补充");
        }
        currentTaskIndex = (currentTaskIndex + 1) % tasks.size();
    }


    /**
     * 判断计划是否为空
     * @return 计划是否为空
     */
    private boolean isEmpty() {
        return tasks == null || tasks.isEmpty();
    }

}
